//index.js
//获取应用实例
const app = getApp()
const txvContext = requirePlugin("tencentVideo");
var util = require("../../utils/utils.js");
Page({
  data: {
    userInfo: {},
    slides: [],
    flag: true,
    Activity: {},
    WonderfulActivity: {},
    showModalStatus: false, //是否登录，默认没有登录
    Wonderful: [],
    Detail: {},
    recommend: [],
    keyword:[],//关键字
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    courseList: [{
        key: "1",
        name: "校本实践",
        imgUrl: "/assets/images/xiaoben.png"
      },
      {
        key: "2",
        name: "城市印象",
        imgUrl: "/assets/images/chengshi.png"
      },
      {
        key: "3",
        name: "军事生存",
        imgUrl: "/assets/images/junshi.png"
      },
      {
        key: "4",
        name: "科学少年",
        imgUrl: "/assets/images/kexue.png"
      },
      {
        key: "5",
        name: "研学中国",
        imgUrl: "/assets/images/yanxue.png"
      },
      // { key: "6", name: "班级定制", imgUrl: "/assets/images/banji.png" }
    ],
    curren: {
      go: "云南夏令营活动",
      customization: "班级定制"
    }
  },
  powerDrawer: function(e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  //中间导航页面跳转
  jump:function(e){
    let types = e.currentTarget.dataset.types;
    if(types == 0){
      wx.navigateTo({
        url: '/pages/StudiesTravel/StudiesTravel?courseType=1',
      })
    } else if (types==1){
      wx.navigateTo({
        url: '/pages/splendid/splendid?index=0',
      })
    } else{
      wx.showToast({
        title: '暂未开放',
        icon: "none"
      })
    }
  },
  util: function(currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });
    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation;

    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })
    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function() {
      // 执行第二组动画 
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },
  //出现
  show: function() {
    this.setData({
      flag: false
    })
  },
  //消失
  hide: function() {
    this.setData({
      flag: true
    })
  },
  //右上角分享功能  
  onShareAppMessage: function(res) {
    var that = this;
    return {
      title: '',
      path: '/pages/index/index',
      success: function(res) {
        // 转发成功  
        that.shareClick();
      },
      fail: function(res) {
        // 转发失败  
      }
    }
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {
    let _this = this
    this.getUserInfo();
    // 首页头部地区接口
    wx.request({
      url: app.httpUrl + '/home/queryCompanyCity',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: res => {
        this.setData({
          selectData: res.data.data
        })
      }
    })
    // 首页轮播接口
    wx.request({
      url: app.httpUrl + '/home/queryHeadBanner',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        this.setData({
          slides: res.data.data
        })
      }
    })
    var that = this;
    // 首页最新课程推荐接口
    wx.request({
      url: app.httpUrl + '/home/queryNewestRecommendCourse',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        var list = res.data.data;
        for (var i in list) {
          list[i].startDate = util.formatDate(list[i].startDate, 'M月D日');
          list[i].endDate = util.formatDate(list[i].endDate, 'M月D日');
        }
        for(let a = 0 ; a<list.length;a++){
          var keyword = list[a].keyword ? list[a].keyword.split("、") : "";
          list[a].keyword = keyword;
        }
        console.log(list)
        _this.setData({
          recommend: list,
        })
      }
    })

    // 精彩活动
    wx.request({
      url: app.httpUrl + '/home/queryWonderfulActivity',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        var list = res.data.data;
        for (var i in list) {
          list[i].startDate = util.formatDate(list[i].startDate, 'M月D日');
          list[i].endDate = util.formatDate(list[i].endDate, 'M月D日');
        }
        _this.setData({
          Wonderful: list
        })
      }

    })


  },
  onReady: function() {

  },
  getUserInfo: function() {
    let that = this;
    if (app.globalData.appUser != null) {
      return;
    }
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function(res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                // 查询正在进行中的活动
                that.getOngoingActivity();
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  getOngoingActivity: function() {
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/queryOngoingActivity',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        if (res.data.code == '000') {
          if (res.data.data != null) {
            that.setData({
              showModalStatus: true,
              OngoingActivity: res.data.data.length > 0 ? res.data.data[0] : null
            });
          }
        }
      }
    })
  },
  tozhibo() {
    let that = this;
    app.globalData.activityId = that.data.OngoingActivity.activityId;
    wx.switchTab({
      url: '/pages/study/study'
    })
  },
  previewImage(event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var list = [src];
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: list // 需要预览的图片http链接列表     
    })
  },
  /**
   * 跳转支付
   */
  go_pay:function(e){
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/apply/apply?productid=' + id,
    })
  },
  /**
   * 跳转详情页
   */
  go_programme:function(e){
    let id = e.currentTarget.dataset.productid;
    let linkType = e.currentTarget.dataset.linktype;
    wx.navigateTo({
      url: '/pages/programme/programme?productid=' + id + '&linkType=' + linkType,
    })
  }

})